/**
 * 操作本地书签的函数集合
 */
const bookmark = {
    // 增加书签或文件夹
    add: function ({
        parentId = '1', // 1:书签栏 2:其它书签
        title = 'new',
        url = undefined, // url为空时创建文件夹
        index = undefined,
    }) {
        console.log('点击了add_folder')
        console.log(title)

        chrome.bookmarks.create({
            'parentId': parentId,
            'title': title,
            'url': url,
            index: index
        }, function (newFolder) {
            console.log("added folder: " + newFolder.title);
        });

    },

    // 获取 书签树
    getTree: function () {
        return new Promise(function (resolve, reject) {
            chrome.bookmarks.getTree(function (list) {
                resolve(list)
            });
        });
    },
    // 获取书签下所有子节点
    getFolder: function (id = '2298') {
        return new Promise(function (resolve, reject) {
            if (!id) {
                return reject();
            }

            chrome.bookmarks.getChildren(id, function (list) {
                resolve(list)
            });
        });
    },
    // 获取一个书签
    getBookmark: function (id = '2298') {
        return new Promise(function (resolve, reject) {
            if (!id) {
                return reject();
            }

            chrome.bookmarks.get(id, function (list) {
                resolve(...list)
            });
        });
    },
    // 获取最近添加的书签
    getRecent: function (unm = 1) {
        return new Promise(function (resolve, reject) {
            chrome.bookmarks.getRecent(unm, function (list) {
                resolve(list)
            });
        });
    },
    // 指定查询条件搜索书签节点
    search: function () { },
    // 更新书签
    updataBookmark: function (id, { title = undefined, url = undefined }) {
        return new Promise(function (resolve, reject) {
            chrome.bookmarks.getRecent(id, { title, url }, function (list) {
                resolve(list)
            });
        });
    },
    // 移动指定的书签节点到指定的位置。
    moveBookmark: function () { },
    // 删除一个书签或者空书签夹。
    removeBookmark: function (id) {
        return new Promise(function (resolve, reject) {
            if (!id) return reject();

            chrome.bookmarks.remove(id, function () {
                console.log('删除成功了');
                resolve()
            });
        });
    },
    // 删除书签夹目录
    removeTree: function () { },
}
